/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : WhatIsThisAction.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 30, 2009  Raghu Sarraff	 		        Created
 */
package lwf.web.struts.action.word;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwf.factory.WordImageFactory;
import lwf.handler.IProcessHandler;
import lwf.handler.english.word.WhatIsThisHandler;
import lwf.web.struts.action.BaseAction;
import net.lwf.util.web.WebUtil;
import net.lwf.web.constant.IPageTitle;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


//public class WhatIsThisAction extends DispatchAction {
public class WhatIsThisAction extends BaseAction {
	
	public ActionForward home(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String type = request.getParameter("wittype");

		WebUtil.removeAttribute(request, "wittype");
		WebUtil.removeAttribute(request, "dataindex");
		WebUtil.removeAttribute(request, "typedata");
		if(type != null) {
			return newdisplay(mapping, form, request, response);
		}
		return mapping.findForward("home");
	} //home	
	
	public ActionForward newdisplay(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		WebUtil.removeAttribute(request, "wittype");
		WebUtil.removeAttribute(request, "dataindex");
		WebUtil.removeAttribute(request, "typedata");
		
		
		String type = request.getParameter("wittype");
		WebUtil.setAttribute(request, "wittype", type);
		List data = WordImageFactory.getInstance().getImageWords(type);
		WebUtil.setAttribute(request, "typedata", data);
		return shownext(mapping, form, request, response);
	} //home
	
	public ActionForward shownext(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
        this.setDataIndex(request, true);
        return processResponse(request, response); 
	}

	public ActionForward displayanswer(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String dataindexS = (String) WebUtil.getAttribute(request, "dataindex");
		int dataIndexI = Integer.parseInt(dataindexS);
		List typedata = (List) WebUtil.getAttribute(request, "typedata");
		String answer = (String) typedata.get(dataIndexI-1);

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<h1>" + answer + "</h1>");
		out.flush();
		return null;
	} //home
	
	private ActionForward processResponse(HttpServletRequest request, 
			                              HttpServletResponse response) throws IOException {
		//Process Data
		String dataindexS = (String) WebUtil.getAttribute(request, "dataindex");
		int dataIndexI = Integer.parseInt(dataindexS);
		String type = (String) WebUtil.getAttribute(request, "wittype");
		List typedata = (List) WebUtil.getAttribute(request, "typedata");
		String data = (String) typedata.get(dataIndexI-1);
		
		WhatIsThisHandler handler = new WhatIsThisHandler(type, dataIndexI-1, typedata.size()-1, data);
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println(handler.getFormattedDisplay());
		out.flush();
		return null;
		
	}
	public ActionForward showprevious(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
        this.setDataIndex(request, false);
        return processResponse(request, response);
	}	
	
	private void setDataIndex(HttpServletRequest request, boolean showNext) {
		String dataindex = (String) WebUtil.getAttribute(request, "dataindex");
		if(dataindex == null) {
			dataindex = "1";
			WebUtil.setAttribute(request, "dataindex", dataindex);
		}
		else {
			int nDataindex = Integer.parseInt(dataindex);
			List typedata = (List) WebUtil.getAttribute(request, "typedata");

			if(showNext) {
				if(typedata.size()-1 == nDataindex) {
					return;
				}
				nDataindex++;
			}
			else {
				if(nDataindex == 0) {
					return;
				}
				nDataindex = nDataindex - 1;
			}
			WebUtil.setAttribute(request, "dataindex", String.valueOf(nDataindex));
		}
	}

	@Override
	public IProcessHandler getHandler(HttpServletRequest request) {
		return null;
	}
	
	@Override
	public String getPageTitle() {
		return IPageTitle.WHAT_IS_THIS;
	}

	@Override
	public String getMainTitle() {
		return null;
	}

	@Override
	public String getPdfLink() {
		return null;
	}

	@Override
	public String getQuizLink() {
		return null;
	}

} //WhatIsThisAction

